#include "Controls/Panels/StackPanel.h"
namespace cxgui
{


cinder::Vec2f StackPanel::GetPreferedSize()
{
	Vec2f size(0.f, 0.f);
	if(_orientation == Orientation::Vertical)
	{
		for(auto control : _controls)
		{
			auto controlSize = control->GetPreferedSize();			
			GrowVect(controlSize, control->GetMargin());
			if(controlSize.x > size.x)
			{
				size.x = controlSize.x;
			}
			size.y += controlSize.y;
		}
	}
	else 
	{
		for(auto control : _controls)
		{
			auto controlSize = control->GetPreferedSize();			
			GrowVect(controlSize, control->GetMargin());
			if(controlSize.y > size.y)
			{
				size.y = controlSize.y;
			}
			size.x += controlSize.x;
		}
	}
	GrowVect(size, _padding);
	return size;
}

void StackPanel::OnClientRectangleChanged()
{
	if(_orientation == Orientation::Vertical)
	{
		auto yOffset = 0.f;
		for(auto control : _controls)
		{
			auto controlPreferedSize = control->GetPreferedSize();
			GrowVect(controlPreferedSize, control->GetMargin());
			auto controlClientArea = _clientRectangle;
			controlClientArea.y1 += yOffset;
			controlClientArea.y2 = controlClientArea.y1 + controlPreferedSize.y;
			yOffset += controlPreferedSize.y;
			control->SetClientRectangle(controlClientArea);
		}
	}
	Panel::OnClientRectangleChanged();
	
}

void StackPanel::Draw()
{
	DrawBackground();
	for(auto control : _controls)
	{
		control->Draw();
	}
}



}